<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class News_category_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }
    function get_all_data($num,$offset,$arr_search)
    {
		/*Begin search*/
		switch($arr_search['field_search'])
		{
			case 'name':
				$this->db->like('name',$arr_search['key_search']);
				break ;
		}
		/*End search*/
		
		$this->db->where('bl_active <>', -1);
		$this->db->where('parent_id', 0);
		$this->db->order_by('nb_order','ASC');
		$query = $this->db->get('news_category',$num,$offset);
		return $query->result();
    }

	function get_num_data($arr_search)
	{
		/*Begin search*/
		switch($arr_search['field_search'])
		{
			case 'name':
				$this->db->like('name',$arr_search['key_search']);
				break ;
		}
		/*End search*/
		$this->db->where('parent_id', 0);
		$this->db->where('bl_active <>', -1);
		$query = $this->db->get('news_category');
		return $query->num_rows();
    }
	
    function get_detail($id)
    {
        $query = $this->db->get_where('news_category', array('cat_id' =>$id));
        return $query->row();
    }
	
	function optionCategoryLevel($cid=0, $value='', $nb_level=4, $pace="|")
	{
		$str = '&nbsp;';
		$this->db->select('cat_id as no, cat_name as name, parent_id') ;
		$query =  $this->db->get_where('news_category', array("parent_id"=>$cid ,'bl_active !=' =>-1));
		$data= $query->result();
		
		if(count($data)>0 && $nb_level>0):
			--$nb_level;
			$pace .= "__";
			foreach($data as $object):
				$str .= "<option value='".$object->no."'".($object->no==$value?" selected=\"selected\"":"").">" . $pace . $object->name. "</option>";
				$str .= $this->optionCategoryLevel($object->no, $value, $nb_level, $pace);
			endforeach;
		endif;
		return $str;
	}   
	 
	function getCategory($parent_id=0)
	{	
		$this->db->where('bl_active',1);
		$this->db->where("parent_id",$parent_id);
        $query = $this->db->get('news_category');
        return $query->result();
    }
	function adminGetCategory($parent_id=0)
	{	
		$this->db->where("parent_id",$parent_id);
        $query = $this->db->get('news_category');
        return $query->result();
    }

   function save($data, $cat_id=0)
   {
		if($cat_id){
			$this->db->where('cat_id',$cat_id);
			if($this->db->update('news_category',$data)){
				 return true;
			}else{
				 return false;
			}
		}else{
			if($this->db->insert('news_category',$data)){
				return true;
			}else{
				return false;
			}
		}
    }
	
    function delete($id)
	{
          $this->db->where('cat_id',$id);
          $query = $this->db->delete('news_category');
          if($query){
              return true;
          }else{
              return false;
          }
    }
	
	function deleted($id)
	{
          $this->db->where('cat_id',$id);
		  $data=array('bl_active'=>'-1');
          $query = $this->db->update('news_category',$data);
          if($query){
              return true;
          }else{
              return false;
          }
    }
	
    function getParentName($cat_id=0) 
	{
        $this->db->select("name");
        $this->db->where('cat_id',$cat_id);
        $query = $this->db->get("news_category");
        return $query->result();
    }
	
   /* function get_list_permit()
	{
        return $this->db->get('permission')->result();
    }
	
	
	function optionCategory($value=0)
	{
		$query = $this->db->get('category');
		$category_arr = $query->result();
		$str_opt = "";
		$str_opt = "<option value=''> Chuyên mục gốc</option>";
		foreach($category_arr as $item_opt){
			$str_opt .="<option value='".$item_opt->cat_id."'".(($value==$item_opt->cat_id)?" selected":"")."> ".$item_opt->name."</option>";
		}
		return $str_opt;
	}
	
    function getAllData($num, $offset)
	{
        $query = $this->db->get('news_category', $num, $offset);
        return $query->result();  
    }*/
	
    /*function getCategoryLevel($parent_id=0)
	{
        $query = $this->db->get('news_category',array("parent_id"=>$parent_id));
        return $query->result();
    }
	
    function getNumData()
	{
        $query = $this->db->get('news_category');
        return $query->num_rows();
    }*/
	
	
}
?>
